#include "AbstractItem.h"



// todo: all data type names to common defines.
namespace
{
QString TYPE_FIELD_NAME = "type";
QString NAME_FIELD_NAME = "name";
QString DESC_FIELD_NAME = "description";
}



AbstractItem::AbstractItem(QObject *parent) : QObject(parent)
{
    d = new Data();
}



void AbstractItem::setData(const int &data_type, const QVariant &data)
{
    if (data_type >= CustomData) return setCustomData(data_type, data);

    switch (data_type)
    {
    case TypeData:
        d->type = data;
        return;

    case NameData:
        d->name = data;
        return;

    case DescData:
        d->desc = data;
        return;
    }
}



QVariant AbstractItem::data(const int &data_type) const
{
    if (data_type >= CustomData) return customData(data_type);

    switch (data_type)
    {
    case TypeData: return d->type;
    case NameData: return d->name;
    case DescData: return d->desc;
    }

    return QVariant();
}



int AbstractItem::fieldCount() const
{
    return (DataType_LAST-DataType_FIRST+1);
}



int AbstractItem::itemCount() const
{
    return items().count();
#if 0
    int ret = 0;
    foreach (const AbstractItem* item, findChildren<AbstractItem*>())
    {
        if (item->parent() != this) continue;
        ret++;
    }

    return ret;
#endif
}



QList<AbstractItem*> AbstractItem::items() const
{
    QList<AbstractItem*> ret;
    foreach (AbstractItem* item, findChildren<AbstractItem*>())
    {
        if (!item || item->parent() != this) continue;

        ret.append(item);
    }

    return ret;
}



AbstractItem* AbstractItem::itemAt(const int& index) const
{
    QList<AbstractItem*> list = items();

    if (list.isEmpty() ||
        index < 0 ||
        index >= list.count()) return 0;

    return list.at(index);
}



QString AbstractItem::fieldName(const int& data_type) const
{
    switch (data_type)
    {
    case TypeData: return TYPE_FIELD_NAME;
    case NameData: return NAME_FIELD_NAME;
    case DescData: return DESC_FIELD_NAME;
    default: return QString::null;
    }
}



int AbstractItem::field(const QString& field_name) const
{
    if (field_name == TYPE_FIELD_NAME) return TypeData;
    if (field_name == NAME_FIELD_NAME) return NameData;
    if (field_name == DESC_FIELD_NAME) return DescData;

    return UndefinedData;
}



int AbstractItem::type() const
{
    QVariant v = data(TypeData);
    if (!v.isValid()) return -1;

    bool ok = false;
    int ret = v.toInt(&ok);

    if (!ok) return -1;

    return ret;

}



QString AbstractItem::name() const
{
    QVariant v = data(NameData);
    if (!v.isValid()) return QString::null;

    return v.toString();
}



QString AbstractItem::desc() const
{
    QVariant v = data(DescData);
    if (!v.isValid()) return QString::null;

    return v.toString();
}



void AbstractItem::setCustomData(const int &, const QVariant &)
{
}



QVariant AbstractItem::customData(const int &) const
{
    return QVariant();
}



